﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class _FIM_MoneyRequestDtls : System.Web.UI.UserControl
    {
        Main main = new Main();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_MoneyRequestDtls mrd = new B_FIM_MoneyRequestDtls();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_BGM_StrategyDataMapping strmap = new B_BGM_StrategyDataMapping();
        IBLL_BGM_MAS_Expense exp = new B_BGM_MAS_Expense();

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void LoadRoot(string strMoneyReqSeq, int nWidth, int nHeight)
        {
            //M_FIM_MoneyRequests info = mr.GetInfo(strMoneyReqSeq);

            //lblBgmSourceSeq.Text = info.bgm_source_seq.ToString();
            //lblBgmYear.Text = strBgmYear;
            //lblBgmMonth.Text = strBgmMonth;
            //sourceInfo = source.GetInfo(strBgmSourceSeq);
            //lblBgmSourceName.Text = sourceInfo.bgm_source_name;
            //lblBgmProjOperExpSeq.Text = strBgmProjOperSeq;

            //LoadDDLCurrencySeq();
            //DropDownList_BgmProjectExpense1.LoadControl(strBgmProjOperSeq, strBgmSourceSeq, 150, null);

            //RadGrid1.Width = Unit.Pixel(nWidth);

            //if (strMoneyReqSeq.Length > 0)
            //{
            //    LoadRadGrid1(strMoneyReqSeq);
            //}
            //else
            //{
            //    LoadRadGrid1("-1");
            //}

            //UpdateUI1("ViewAll");
        }

        protected void LoadRadGrid1(string money_req_seq)
        {
            //dtlsInfoList = mrd.GetFull(money_req_seq);
            //RadGrid1.DataSource = dtlsInfoList;
            //RadGrid1.DataBind();
            //SumAmount();
        }

        protected void LoadDDLCurrencySeq()
        {
            //ddlCurrencySeq.DataSource = cur.GetList();
            //ddlCurrencySeq.DataBind();
        }

        protected void SumAmount()
        {
            //double dSumAmount = 0;
            //foreach (M_FIM_MoneyRequestDtls info in dtlsInfoList)
            //{
            //    dSumAmount = dSumAmount + (info.amount_req == null ? 0 : (double)info.amount_req);
            //}
            //lblSumAmount.Text = "รวมเป็นจำนวนเงิน : " + dSumAmount.ToString() + " บาท";
        }

        protected void UpdateUI1(string strCommand)
        {
            //switch (strCommand)
            //{
            //    case "ViewAll":
            //    case "Insert":
            //    case "Update":
            //        cmdAddNew.Visible = true;
            //        cmdEdit.Visible = true;
            //        cmdDelete.Visible = true;
            //        cmdCancel.Visible = false;
            //        cmdInsert.Visible = false;
            //        cmdUpdate.Visible = false;

            //        Panel1.Visible = true;
            //        PanelEdit.Visible = false;
            //        break;
            //    case "AddNew":
            //        cmdAddNew.Visible = false;
            //        cmdEdit.Visible = false;
            //        cmdDelete.Visible = false;
            //        cmdCancel.Visible = true;
            //        cmdInsert.Visible = true;
            //        cmdUpdate.Visible = false;

            //        Panel1.Visible = false;
            //        PanelEdit.Visible = true;
            //        break;
            //    case "Edit":
            //        cmdAddNew.Visible = false;
            //        cmdEdit.Visible = false;
            //        cmdDelete.Visible = false;
            //        cmdCancel.Visible = true;
            //        cmdInsert.Visible = false;
            //        cmdUpdate.Visible = true;

            //        Panel1.Visible = false;
            //        PanelEdit.Visible = true;
            //        break;
            //}
        }

        protected void ClearControl()
        {
            //txtMoneyReqDtlsName.Text = "";
            //txtBathAmount.Text = "";
            //txtExchangeRate.Text = "";
            //txtAmount.Text = "";
            //lblIndex.Text = "";
            //lblError.Visible = false;
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            //if (e.CommandName == "RowClick" && e.Item is GridDataItem)
            //{
            //    e.Item.Selected = true;
            //    string strReceiveSeq = e.Item.OwnerTableView.Items[e.Item.ItemIndex]["receive_seq"].Text;
            //    Session["ReceiveSeq"] = strReceiveSeq;
            //    LoadRadGrid2(strReceiveSeq);
            //}
        }

        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            UpdateUI1("AddNew");
        }

        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            if (RadGrid1.SelectedItems.Count > 0)
            {
                UpdateUI1("Edit");

                //string strMoneyReqDtlsSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["money_req_dtls_seq"].Text;
                //string strMoneyReqSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["money_req_seq"].Text;
                //string strMoneyReqDtlsName = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["money_req_dtls_name"].Text;
                //string strBgmPlanSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bgm_plan_seq"].Text;
                string strBgmExpSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bgm_exp_seq"].Text;
                string strPercentVat = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["percent_vat"].Text;
                string strPercentTax = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["percent_tax"].Text;
                //string strBathAmount = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bath_amount"].Text;
                //string strBathAmountVat = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bath_amount_vat"].Text;
                //string strBathAmountTax = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["bath_amount_tax"].Text;
                //string strCurrencySeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["currency_seq"].Text;
                //string strExchangeRate = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["exchange_rate"].Text;
                string strAmount = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["amount"].Text;
                string strAmountVat = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["amount_vat"].Text;
                string strAmountTax = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["amount_tax"].Text;

                lblIndex.Text = RadGrid1.SelectedItems[0].ItemIndex.ToString();
                //txtMoneyReqDtlsName.Text = main.WebString(strMoneyReqDtlsName);
                //ddlCurrencySeq.SelectedValue = main.WebString(strCurrencySeq);
                //txtBathAmount.Text = main.WebString(strBathAmount);
                //txtExchangeRate.Text = main.WebString(strExchangeRate);
                txtAmount.Text = main.WebString(strAmount);
            }
        }

        protected void cmdDelete_Click(object sender, EventArgs e)
        {
            //if (RadGrid1.SelectedItems.Count > 0)
            //{
            //    string strMoneyReqDtlsSeq = RadGrid1.Items[RadGrid1.SelectedItems[0].ItemIndex]["money_req_dtls_seq"].Text;
            //    if (strMoneyReqDtlsSeq.Length > 0)
            //    {
            //        M_FIM_MoneyRequestDtls info = new M_FIM_MoneyRequestDtls();
            //        info.money_req_dtls_seq = Convert.ToInt32(strMoneyReqDtlsSeq);
            //        info.updated_by = main.ConvertNullInt(Session["EmpSeq"].ToString());
            //        info.updated_when = DateTime.Now;

            //        mrd.Delete(info);
            //    }

            //    dtlsInfoList.RemoveAt(RadGrid1.SelectedItems[0].ItemIndex);

            //    RadGrid1.DataSource = dtlsInfoList;
            //    RadGrid1.DataBind();
            //    UpdateUI1("ViewAll");
            //    SumAmount();
            //}
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            ClearControl();
            UpdateUI1("ViewAll");
        }

        protected void cmdInsert_Click(object sender, EventArgs e)
        {
            ////string strMoneyReqDtlsName = txtMoneyReqDtlsName.Text;
            //string strBgmPlanSeq = TreeView_BgmPlan1.GetSelectedValue();
            //string strBathAmount;
            //string strAmount;
            //string strCurrencySeq;
            ////string strCurrencyName;
            //string strExchangeRate;
            //string strAmountReq;

            //if (strBgmPlanSeq.Length == 0 || TreeView_BgmPlan1.GetSelectedNode().Category != "Plan")
            //{
            //    lblError.Text = "* กรุณาเลือกแผนการใช้จ่าย!!";
            //    lblError.Visible = true;
            //    return;
            //}

            //strBathAmount = txtBathAmount.Text;
            //strAmount = txtAmount.Text;
            //strCurrencySeq = ddlCurrencySeq.SelectedValue;
            ////strCurrencyName = ddlCurrencySeq.SelectedItem.Text;
            //strExchangeRate = txtExchangeRate.Text;
            ////strAmountReq = (Convert.ToDouble(strForeignAmount) * Convert.ToDouble(strExchangeRate)).ToString();

            //M_FIM_MoneyRequestDtls info = new M_FIM_MoneyRequestDtls();
            ////info.money_req_dtls_name = strMoneyReqDtlsName;
            //info.amount = main.ConvertNullDouble(strAmount);
            //info.currency_seq = main.ConvertNullInt(strCurrencySeq);
            //info.exchange_rate = main.ConvertNullDouble(strExchangeRate);
            //info.bath_amount = main.ConvertNullDouble(strBathAmount);
            ////info.amount_req = main.ConvertNullDouble(strAmountReq);

            //dtlsInfoList.Add(info);
            //RadGrid1.DataSource = dtlsInfoList;
            //RadGrid1.DataBind();
            //UpdateUI1("Insert");
            //ClearControl();
            //SumAmount();
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            //int nIndex = (lblIndex.Text.Length > 0 ? Convert.ToInt32(lblIndex.Text) : 0);
            ////string strMoneyReqDtlsName = txtMoneyReqDtlsName.Text;
            //string strBgmPlanSeq = TreeView_BgmPlan1.GetSelectedValue();
            //string strBathAmount;
            //string strAmount;
            //string strCurrencySeq;
            //string strCurrencyName;
            //string strExchangeRate;
            //string strAmountReq;

            //if (strBgmPlanSeq.Length == 0 || TreeView_BgmPlan1.GetSelectedNode().Category != "Plan")
            //{
            //    lblError.Text = "* กรุณาเลือกแผนการใช้จ่าย!!";
            //    lblError.Visible = true;
            //    return;
            //}

            //strBathAmount = txtBathAmount.Text;
            //strAmount = txtAmount.Text;
            //strCurrencySeq = ddlCurrencySeq.SelectedValue;
            ////strCurrencyName = ddlCurrencySeq.SelectedItem.Text;
            //strExchangeRate = txtExchangeRate.Text;
            ////strAmountReq = (Convert.ToDouble(strForeignAmount) * Convert.ToDouble(strExchangeRate)).ToString();

            ////M_FIM_ReceiveDtls info = dtlsInfoList[nIndex];
            ////dtlsInfoList[nIndex].money_req_dtls_name = strMoneyReqDtlsName;
            ////dtlsInfoList[nIndex].bgm_plan_seq = main.ConvertNullInt(strBgmPlanSeq);
            ////dtlsInfoList[nIndex].bath_amount = main.ConvertNullDouble(strBathAmount);
            ////dtlsInfoList[nIndex].bath_amount_real = main.ConvertNullDouble(strBathAmount);
            ////dtlsInfoList[nIndex].currency_seq = main.ConvertNullInt(strCurrencySeq);
            ////dtlsInfoList[nIndex].currency_name = strCurrencyName;
            ////dtlsInfoList[nIndex].exchange_rate = main.ConvertNullDouble(strExchangeRate);
            ////dtlsInfoList[nIndex].foreign_amount = main.ConvertNullDouble(strForeignAmount);
            ////dtlsInfoList[nIndex].amount_req = main.ConvertNullDouble(strAmountReq);

            //RadGrid1.DataSource = dtlsInfoList;
            //RadGrid1.DataBind();
            //UpdateUI1("Update");
            //ClearControl();
            //SumAmount();
        }

        public void Insert(string strMoneyReqSeq)
        {
            //foreach (M_FIM_MoneyRequestDtls info in dtlsInfoList)
            //{
            //    info.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
            //    mrd.Insert(info);
            //}
        }

        public void Update(string strMoneyReqSeq)
        {
            //int i = 0;
            //foreach (M_FIM_MoneyRequestDtls info in dtlsInfoList)
            //{
            //    info.money_req_seq = main.ConvertNullInt(strMoneyReqSeq);
            //    if (RadGrid1.Items[i]["money_req_dtls_seq"].Text.Length == 0)
            //    {
            //        mrd.Insert(info);
            //    }
            //    else
            //    {
            //        mrd.Update(info);
            //    }

            //    i++;
            //}
        }
    }
}